using System;
using System.Collections.Generic;

using System.ComponentModel;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace GURUCORE.Portal.AspNet.Control
{	
	[DefaultProperty("Value"),
		ToolboxData("<{0}:Calendar runat=server></{0}:Calendar>")]
    [System.Drawing.ToolboxBitmap(typeof(TextBoxS), "resGURUCORE.ico")]
	public class ___Calendar : System.Web.UI.WebControls.TextBox,IPostBackDataHandler
	{
	
        private string _ScriptsPath;
        private string _ImageURL;
        
        private string _DateFormat;
        private string _Theme;
        private string _Language;
        //private TextBox txtCalendar = new TextBox();
        private Image imgCalendar = new Image();
        private Literal ltrCalendar = new Literal();
        public event EventHandler Change;
		
        [Bindable(true),
        Category("Appearance"),
        DefaultValue("EN")]
        public string Language
        {
            get
            {
                return (null == _Language) ? "EN" : _Language;
            }

            set
            {
                _Language= value;
            }
        }
        [Bindable(true),
        Category("Appearance"),
        DefaultValue("calendar-Blue")]
        public string Theme
        {
            get
            {
                return (null == _Theme) ? "calendar-Blue" : _Theme;
            }

            set
            {

                _Theme= value;
            }
        }
        [Bindable(true),
        Category("Appearance"),
        DefaultValue("dd/MM/yyyy")]
        public string DateFormat
        {
            get
            {
				string sDatePattern = System.Threading.Thread.CurrentThread.CurrentCulture.DateTimeFormat.ShortDatePattern;
				if (sDatePattern.IndexOf("MM") < 0)
				{
					sDatePattern = sDatePattern.Replace("M","MM");
				}
				if (sDatePattern.IndexOf("dd") < 0)
				{
					sDatePattern = sDatePattern.Replace("d","dd");
				}
                return (null == _DateFormat ) ? sDatePattern : _DateFormat;
            }

            set
            {
                _DateFormat= value;
            }
        }
        [Bindable(true),
        Category("Appearance"),
        DefaultValue("js/calendar/")]
        public string ScriptsBasePath
        {
            get
            {
                return (null == _ScriptsPath) ? "js/calendar/" : _ScriptsPath;
			}

            set
            {
                _ScriptsPath = (null == value) ? "js/calendar/" : value;
            }
        }
        [Bindable(true),
        Category("Appearance"),
        DefaultValue("js/calendar/img.gif")]
        public string ImageURL
        {
            get
            {
                return (null == _ImageURL) ? "js/calendar/img.gif" : _ImageURL;
            }

            set
            {
                _ImageURL = (null == value) ? "js/calendar/img.gif" : value;
            }
        }
		/// <summary>
		/// Render this control to the output parameter specified.
		/// </summary>
		/// <param name="output"> The HTML writer to write out to </param>
		protected override void Render(HtmlTextWriter output)
		{
            EnsureChildControls ();
            System.Web.UI.Page _page =(System.Web.UI.Page) System.Web.HttpContext.Current.Handler;

            // if ( !_page.IsClientScriptBlockRegistered("Calendar") ) 
            if (!_page.ClientScript.IsClientScriptBlockRegistered("Calendar")) 
            {
                //theme
                StringBuilder script = new StringBuilder();
                script.Append("<LINK title='");
                script.Append(Theme);
                script.Append("' media='all' href='");
                script.Append(ScriptsBasePath);
                script.Append(Theme);
                script.Append(".css' type='text/css' rel='stylesheet'>");
			    
                script.Append("<script src='" );
                script.Append(ScriptsBasePath);
                script.Append("calendar.js' type='text/javascript'></script>\n");
                
                script.Append("<script src='");
                script.Append(ScriptsBasePath);
                script.Append("lang/calendar-");
                script.Append(Language);
                script.Append(".js' type='text/javascript'></script>\n");

                script.Append("<script src='" );
                script.Append(ScriptsBasePath);
                script.Append("calendar-setup.js' type='text/javascript'></script>\n");
                script.Append("<script> var today = new Date(); </script> \n");

                _page.ClientScript.RegisterClientScriptBlock(this.GetType(),"Calendar", script.ToString());
                // _page.RegisterClientScriptBlock("Calendar", script.ToString());
                output.Write(script);
            }
			this.ReadOnly = true;
            base.Render(output);
            output.Write(" <img src = '"+ImageURL+"' ID = '"+this.ClientID+"img' border = 0 style = 'cursor:hand'>");
            StringBuilder str = new StringBuilder();
            str.Append("\n<script> \n");             
            str.Append("Calendar.setup({ \n");
            str.Append("inputField     :    '"+this.ClientID+"', \n");
            str.Append("ifFormat : '"+ConvertToJSDateFormat(DateFormat)+"',\n");
            str.Append("button         :    '"+this.ClientID+"img', \n");
            str.Append("align          :    'B1',\n");
            str.Append("singleClick    :    true,\n");
            str.Append("dateStatusFunc :    function (date) {\n");
            str.Append("return  false;\n");
            str.Append("}\n");
            str.Append("})\n;");
            str.Append("</script> \n");

            output.Write(str);
   

		}
        
        string ConvertToJSDateFormat (string f)
        {
            f =  f.Replace ("dd","%d");
            f= f.Replace("MM","%m");
            f= f.Replace("yyyy","%Y");
            return f;

        }
          
        protected override void CreateChildControls()
        {
//            txtCalendar.Text = Value;
//            txtCalendar.EnableViewState = true;
//            txtCalendar.ID = "txt"+this.ID;
//            txtCalendar.CssClass = CssClass;
            imgCalendar.ImageUrl = ImageURL;
            imgCalendar.ID = "img"+this.ID;
            imgCalendar.EnableViewState= true;
            
            
           
           
            //Controls.Add(txtCalendar);    
            Controls.Add(imgCalendar);
           
          

        }
        protected override void  RaisePostDataChangedEvent()
        {
           OnChange(EventArgs.Empty);
           base.RaisePostDataChangedEvent();
        }
        
        public virtual void OnChange(EventArgs e)
        {
            if (Change!= null)
                Change(this,e);
        }
        
	}
    
}
